aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/send/invoice/[id]/+page.server.js
blob: 3db03123c8fdc22bea9531942a55e472adf08cb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { error, fail, redirect } from '@sveltejs/kit';
import { fd, auth, get, post } from '$lib/utils';

export async function load({ params: { id }, parent }) {
	let { user } = await parent();

	let { amount, address, tip, text, user: recipient } = await get(`/invoice/${id}`);
	if (recipient.username === user.username) throw error(500, { message: 'Cannot send to self' });
	if (tip) amount += tip;
	return { amount, address, payreq: text, recipient };
}

export const actions = {
	default: async ({ cookies, params: { hash }, request }) => {
		try {
			let body = await fd(request);
			body.amount = parseInt(body.amount);
			body.hash = hash;

			await post('/payments', body, auth(cookies));
		} catch (e) {
			return fail(400, { message: e.message });
		}

		throw redirect(307, '/sent');
	}
};